package com.ocom.ugcapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.entity.card.EntityXfRecordWaitpay;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.repairpay.GetRepairPayRecordListRequest;
import com.ocom.common.request.payconfig.repairpay.RepairPayRecordRechargeRequest;
import com.ocom.common.request.payhandle.BackPayRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.ugcapi.client.PayConfigClientService;
import com.ocom.ugcapi.client.PayHandleClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;


@RestController
@Slf4j
public class RepairPayController {

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private PayHandleClientService payHandleClientService;

    private ExecutorService executor = Executors.newSingleThreadExecutor();


    @PostMapping(WxServiceUrl.URL_RepairPayRecordQuery)
    public Result repairPayRecordQuery(@RequestBody Map<String, Object> map) {
        GetRepairPayRecordListRequest request = JSON.parseObject(JSON.toJSONString(map), GetRepairPayRecordListRequest.class);
        request.setComId(request.getUserInfo().getComId());
        request.setPId(request.getUserInfo().getPId());
        request.setStatus(0);
        return payConfigClientService.getRepairPayRecordList(request);
    }

    @PostMapping(WxServiceUrl.URL_RepairPayRecordRecharge)
    public Result repairPayRecordRecharge(@RequestBody Map<String, Object> map) {
        log.info("=====================first======================:补缴 【{}】", map);

        RepairPayRecordRechargeRequest request = JSON.parseObject(JSON.toJSONString(map), RepairPayRecordRechargeRequest.class);
        request.setComId(request.getUserInfo().getComId());
        request.setPId(request.getUserInfo().getPId());

        GetRepairPayRecordListRequest getRepairPayRecordListRequest = new GetRepairPayRecordListRequest();
        getRepairPayRecordListRequest.setComId(request.getComId());
        getRepairPayRecordListRequest.setPId(request.getPId());
        getRepairPayRecordListRequest.setStatus(0);
        Result repairPayRecordList = payConfigClientService.getRepairPayRecordList(getRepairPayRecordListRequest);

        if (200 != repairPayRecordList.getCode()) {
            return ResultUtil.error(ResultEnum.Err_1001);
        }

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(repairPayRecordList.getData()));
        List<EntityXfRecordWaitpay> entityXfRecordWaitpays = JSON.parseArray(JSON.toJSONString(jsonObject.get("list")), EntityXfRecordWaitpay.class);
        Map<Long, EntityXfRecordWaitpay> collect = entityXfRecordWaitpays.stream().collect(Collectors.toMap(EntityXfRecordWaitpay::getId, EntityXfRecordWaitpay -> EntityXfRecordWaitpay));

        Result result = ResultUtil.success();
        EntityXfRecordWaitpay waitpay = collect.get(request.getId());
        if (null != waitpay) {
            log.info("=====================start======================:补缴线程 【{}】", waitpay.getOrderNo());

//            EntityXfRecordWaitpay entityXfRecordWaitpay = new EntityXfRecordWaitpay();
//            entityXfRecordWaitpay.setComId(waitpay.getComId());
//            entityXfRecordWaitpay.setOrderNo(waitpay.getOrderNo());
//            entityXfRecordWaitpay.setPayState(0);
//            Result updatePayState = payConfigClientService.updatePayState(entityXfRecordWaitpay);
//            if (200 != updatePayState.getCode()) {
//                return updatePayState;
//            }
            BackPayRequest backPayRequest = new BackPayRequest();
            backPayRequest.setBackId(waitpay.getId());
            backPayRequest.setCompanyId(waitpay.getComId());
//            backPayRequest.setAmount(waitpay.getConsMoney().intValue());
//            backPayRequest.setUserId(waitpay.getPId());
//            backPayRequest.setMerchId(waitpay.getMerchId());
//            backPayRequest.setConsTime(waitpay.getConsTime());
//            backPayRequest.setPlatOrderNo(waitpay.getOrderNo());
//            backPayRequest.setPointId(waitpay.getPointId());
//            backPayRequest.setConsType(waitpay.getConsType());
//            backPayRequest.setMachNo(waitpay.getDeviceNo());
//            backPayRequest.setDeviceNo(waitpay.getDeviceNo());
//            backPayRequest.setDeviceName(waitpay.getDeviceName());
            backPayRequest.setTradeNo("BJ" + new Date().getTime() + "C" + waitpay.getComId() + "P" + waitpay.getPId() + "M" + RandomUtil.generateNumber(4));
            result = payHandleClientService.backPay(backPayRequest);

            log.info("=====================end======================:补缴，ID：【{}】, 【{}】", waitpay.getPId(), result);
        } else {
            result = ResultUtil.error(ResultEnum.Err_805);
        }

        log.info("=====================over======================:补缴线程执行结束");
        repairPayRecordList = payConfigClientService.getRepairPayRecordList(getRepairPayRecordListRequest);
        jsonObject = JSON.parseObject(JSON.toJSONString(repairPayRecordList.getData()));
        entityXfRecordWaitpays = JSON.parseArray(JSON.toJSONString(jsonObject.get("list")), EntityXfRecordWaitpay.class);
        if (null == entityXfRecordWaitpays || 0 == entityXfRecordWaitpays.size()) {
            Result account = payConfigClientService.GetXFAccount(waitpay.getComId(), waitpay.getPId().intValue(),-1);
            if (200 == account.getCode()) {
                EntityXfAccountInfo entityXfAccountInfo = JSON.parseObject(JSON.toJSONString(account.getData()), EntityXfAccountInfo.class);
                entityXfAccountInfo.setAccountState(0);
                Result updateXFAccount = payConfigClientService.UpdateXFAccount(entityXfAccountInfo);
                log.info("修改账户状态【{}】", updateXFAccount);
            } else {
                log.info("获取账户失败【{}】", account);
            }
        }
        return result;
    }

}
